<?php 
$GLOBALS['Md'] = "TimeClock";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
print heade(); 
print body(); 
module($BSD['DB00']);
office_header($_SESSION[$SESSOF]);
include($GLOBALS['PAD']."Modules/TimeClock/main.obj.php");
include($GLOBALS['PAD']."Modules/TimeClock/nav.views.php");
print NavBack();

if (isset($_POST['User']) && $_POST['User'] != '') {
	$sysTime = date("Y-m-d H:i:s");
	$QProfile = "
	INSERT INTO timeclock_profiles 
	(main_users_ref, delay, mon1, mon2, mon3, mon4, tolerance, 
	Creat_Time, TimeIni, TimeEnd, mon5, mon6, mon0, ProfileType)
		VALUES 
	({$_POST['User']},{$_POST['delay']},{$_POST['Mon1']},{$_POST['Mon2']},{$_POST['Mon3']},
	{$_POST['Mon4']},'{$_POST['tolerance']}','{$sysTime}','{$sysTime}','3000-00-00 00:00:00',
	{$_POST['Mon5']},{$_POST['Mon6']},{$_POST['Mon0']},{$_POST['Type']})";
	$GLOBALS['CONN']->db_query($QProfile);

} 
?>
<table width="100%"  border="0" cellpadding="10">
  <tr>
    <td>
	<table width="100%"  border="0" cellpadding="3" cellspacing="1">
      <form action="?" method="get" name="getData">
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">
		  <input type="submit" name="CreateProfile" value="Novo Funcionário"></td>
        </tr>
      </form>
    </table>
	<?php 
	if (isset($_GET['CreateProfile'])) { ?>
	<table width="100%"  border="0" cellpadding="3" cellspacing="1">
   <form action="?" method="post" name="GenProfile">
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Utilizador:</td>
          <td bgcolor="#ACB1CC">
	<select name="User" id="User">
            <?php

	$CUser = "SELECT ref, name FROM main_users
						WHERE ref NOT IN (SELECT main_users_ref FROM timeclock_profiles)
						AND Type = 2
						ORDER BY name ASC";
	$User = $GLOBALS['CONN']->db_array($CUser); 
	for ($o = 1; $o <= $User[0]; $o++) {

       echo '<option value="'.$User[$o]['ref'].'"';
		if (isset($_GET['User']) && $_GET['User'] == $User[$o]['ref']) { 
		echo "selected";}
		echo '>'.$User[$o]['name'].'</option>';
         } ?>
     </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Tipo:</td>
          <td bgcolor="#ACB1CC"><select name="Type" id="Type">
            <option value="1">Normal</option>
            <option value="2">Isento</option>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Tolerancia: </td>
          <td bgcolor="#ACB1CC"><input name="tolerance" type="text" id="tolerance" value="00-00-00 00:15:00"></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Atraso:</td>
          <td bgcolor="#ACB1CC"><input name="delay" type="text" id="delay" value="1200"></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Segunda:</td>
          <td bgcolor="#ACB1CC">
	<select name="Mon1" id="Mon1">
    <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon1']) && $_GET['Mon1'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' �s '.$T[$o]['leavetime'].'</option>';
       } ?>
              </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">TerÇa: </td>
          <td bgcolor="#ACB1CC"><select name="Mon2" id="select2">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon2']) && $_GET['Mon2'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Quarta:</td>
          <td bgcolor="#ACB1CC"><select name="Mon3" id="select3">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon3']) && $_GET['Mon3'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Quinta:</td>
          <td bgcolor="#ACB1CC"><select name="Mon4" id="select4">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon4']) && $_GET['Mon4'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Sexta:</td>
          <td bgcolor="#ACB1CC"><select name="Mon5" id="select5">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon5']) && $_GET['Mon5'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Sábado:</td>
          <td bgcolor="#ACB1CC"><select name="Mon6" id="select6">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon6']) && $_GET['Mon6'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">Domingo:</td>
          <td bgcolor="#ACB1CC"><select name="Mon0" id="select7">
            <?php
	   echo '<option value="0"> </option>';
	$CT = "SELECT ref, entrytime, leavetime FROM timeclock_ttables
						ORDER BY ref ASC";
	$T = $GLOBALS['CONN']->db_array($CT); 
	for ($o = 1; $o <= $T[0]; $o++) {
       echo '<option value="'.$T[$o]['ref'].'"';
		if (isset($_GET['Mon0']) && $_GET['Mon0'] == $T[$o]['ref']) { 
		echo "selected";}
		echo '>Das '.$T[$o]['entrytime'].' às '.$T[$o]['leavetime'].'</option>';
       } ?>
                    </select></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC">&nbsp;</td>
          <td bgcolor="#ACB1CC">
		  <input type="submit" name="CreateProfile" value="Gerar"></td>
        </tr>
      </form>
    </table>
	<?php } ?>
      <br>  
	<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
	<select name="Offices">
   	<?php
	echo '<option value="">Todos</option>';

	$QCallOffice = "SELECT * FROM main_offices WHERE Type IN (1,2,5) = '1' ORDER BY name ASC";
	$CallOffice = $GLOBALS['CONN']->db_array($QCallOffice); 
	for ($o = 1; $o <= $CallOffice[0]; $o++) {

       echo '<option value="'.$CallOffice[$o]['ref'].'"';
		if (isset($_GET['Offices']) && $_GET['Offices'] == $CallOffice[$o]['ref']) { 
		echo "selected";}
		echo '>'.$CallOffice[$o]['name'].'</option>';
         } ?>
      </select>
      <input type="submit" name="Submit" value="Filtrar">	
      <table width="100%"  border="0" cellpadding="3" cellspacing="1">
        <tr bgcolor="#F2F2F2">
          <td width="20%">Funcionário</td>
          <td width="20%">Departamento</td>
		  <td width="15%">Username</td>
		  <td width="15%">Password</td>
          <td width="15%">Tipo</td>
          <td width="2%">Seg.</td>
		  <td width="2%">Ter.</td>
		  <td width="2%">Qua.</td>
		  <td width="2%">Qui.</td>
		  <td width="2%">Sex.</td>
		  <td width="2%">Sab.</td>
        </tr>
   <?php
   if (isset($_POST['Offices'])) {
   $Of = " AND main_offices.ref = {$_POST['Offices']}";
   } else {
   $Of = "";
   }
    $QProfiles = "SELECT main_offices.name AS office,
						 main_users.name AS name,
						 main_users.username,
						  main_users.password,
						a.ref,
						a.main_users_ref,
						a.delay,
						a.mon1,
						a.mon2,
						a.mon3,
						a.mon4,
						a.mon5,
						a.mon6,
						a.mon0,
						a.ProfileType
				FROM timeclock_profiles AS a
		INNER JOIN main_users ON main_users.ref = a.main_users_ref
		INNER JOIN main_offices ON main_users.ref_office = main_offices.ref
		WHERE main_users.Type != 5
		{$Of}
		ORDER BY office ASC, name ASC";
	$Profiles = $GLOBALS['CONN']->db_array($QProfiles); 
	for ($f = 1; $f <= $Profiles[0]; $f++) { 
	
		$line1 = '<tr bgcolor="#D3D6E4">';
		$line2 = '<td  NOWRAP>'.$Profiles[$f]['name'].'</td>';
		$line2 .= '<td NOWRAP>'.$Profiles[$f]['office'].'</td>';
	
		print $line1.$line2;
		echo '<td NOWRAP>'.$Profiles[$f]['username'].'</td>
		<td NOWRAP>'.$Profiles[$f]['password'].'</td>
		<td NOWRAP><select name="ProfileType" id="ProfileType">
		<option value="1"';
		if ($Profiles[$f]['ProfileType'] == 1) {print 'selected'; } 
		echo '>Normal</option>
		<option value="2"';
		if ($Profiles[$f]['ProfileType'] == 2) {print 'selected'; } 
		echo '>Isento</option>
		</select></td>
		<td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon1']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td><td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon2']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td><td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon3']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td><td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon4']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td><td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon5']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td><td NOWRAP>';
		$TimeTable = $GLOBALS['CONN']->db_array("SELECT HOUR(entrytime)AS h, HOUR(leavetime) AS l FROM timeclock_ttables  WHERE ref = '{$Profiles[$f]['mon6']}'"); 
		if (isset($TimeTable[1]['h'])) { echo 'Das '.$TimeTable[1]['h'].' �s '.$TimeTable[1]['l'];}
		echo '</td>
		</tr>';
   } ?>
    </table>    </tr>
	</form>
</table>
<?php echo footer();?>